package com.example.smart.dialog

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.View
import com.example.lib.extend.getDeviceWidth
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.AppUtils
import com.example.lib.widget.wheelview.annotation.ScrollState
import com.example.lib.widget.wheelview.contract.OnWheelChangedListener
import com.example.lib.widget.wheelview.widget.WheelView
import com.example.smart.R
import com.example.smart.base.dialog.base.BaseDialog
import com.example.smart.databinding.DialogDateBinding

class DateDialog(val activity: Activity) : BaseDialog<DialogDateBinding>(dialogContext = activity,
    width = (getDeviceWidth(activity) * 0.95).toInt(),
    gravity = Gravity.BOTTOM) {

    var onResult: ((Int, String) -> Unit)? = null

    private var mType: Int = 0

    override fun setLayoutRes() = R.layout.dialog_date

    override fun afterViewCreate(view: View?) {
        val year = mutableListOf<Int>()
        for (i in 0..100) {
            year.add(2025 - i)
        }

        val month = mutableListOf<Int>()

        for (i in 0..11) {
            month.add(i + 1)
        }

        binding.DialogWheel1.setOnWheelChangedListener(object : OnWheelChangedListener() {
            override fun onWheelSelected(view: WheelView?, position: Int) {

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                binding.DialogWheel2.isEnabled = !(state == ScrollState.SCROLLING || state == ScrollState.DRAGGING)
            }
        })

        binding.DialogWheel2.setOnWheelChangedListener(object : OnWheelChangedListener() {
            override fun onWheelSelected(view: WheelView?, position: Int) {

            }

            override fun onWheelScrollStateChanged(view: WheelView?, state: Int) {
                binding.DialogWheel1.isEnabled = !(state == ScrollState.SCROLLING || state == ScrollState.DRAGGING)
            }
        })

        binding.DialogWheel1.data = year
        binding.DialogWheel2.data = month

        binding.DialogClose.setOnDoubleClickListener {
            dismiss()
        }

        binding.DialogConfirm.setOnDoubleClickListener {
            dismiss()
            onResult?.invoke(mType,
                "${year[binding.DialogWheel1.currentPosition]}${context.getString(R.string.year)}${month[binding.DialogWheel2.currentPosition]}${
                    context.getString(R.string.month)
                }")
        }
    }

    fun show(type: Int) {
        mType = type
        AppUtils.packUpInputKey(activity = activity)
        super.show()
    }

}